<template>
  <div>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="isOpen" width="500px" append-to-body @close='closeDialog'>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运输单位类型" prop="transportUnitTypeId">
          <el-input v-model="form.transportUnitTypeId" placeholder="请输入运输单位类型" />
        </el-form-item>
        <el-form-item label="重量能力" prop="weightCapacity">
          <el-input v-model.number="form.weightCapacity" placeholder="请输入重量能力" />
        </el-form-item>
        <el-form-item label="重量计量单位" prop="uomForWeight">
          <el-input v-model="form.uomForWeight" placeholder="请选择或输入重量计量单位" />
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="showList('Weight')">请选择</el-button>
        </el-form-item>
        <el-form-item label="是否删除" prop="delFlag" v-show="false">
          <el-input v-model="form.delFlag" />
        </el-form-item>
        <el-form-item label="唯一标识" prop="rowkey" v-show="false">
          <el-input v-model="form.rowkey" />
        </el-form-item>
        <el-form-item label="体积容量" prop="volumeCapacity">
          <el-input v-model.number="form.volumeCapacity" placeholder="请输入体积容量" />
        </el-form-item>
        <el-form-item label="体积计量单位" prop="uomForVolume">
          <el-input v-model="form.uomForVolume" placeholder="请选择或输入体积计量单位" />
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="showList('Volume')">请选择</el-button>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import { listTransport,listIsoUnit,getTransport,delTransport,addAndUpdateTransport,importTemplate,exportTransport,addTransport,updateTransport } from "@/api/erp/transport/transport";
  export default {
    name: 'addOrEdit',
    props:{
      //
      addOrEditForm:Object,
      form:Object,
      //显示title
      title:String,
      //是否显示
      open: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    data(){
      return{
        isOpen: this.open,
        isounit:{
          open:false,
          title:"",
          unitType:"",
          type:"addOrEdit",
        },
        // 表单校验
        rules: {
          transportUnitTypeId: [
            { required: true, message: "运输单位类型不能为空", trigger: "blur" }
          ],
          weightCapacity: [
            { type: 'number',required: true, message: "请填写数字", trigger: "blur" }
          ],
          volumeCapacity: [
            { type: 'number',required: true, message: "请填写数字", trigger: "blur" }
          ],
          uomForWeight: [
            { required: true, message: "重量计量单位不能为空", trigger: "blur" }
          ],
          uomForVolume: [
            { required: true, message: "体积计量单位不能为空", trigger: "blur" }
          ]
        }
      }
    },
    created() {
      // this.getList();
    },
    watch:{
      open:function(newVal,oldval){
        console.info(newVal)
        this.isOpen = newVal;
      },
      addOrEditForm:{
        handler: function(val) {
          this.fuzhi(val);
          // if(this.selectCode.type=="addOrEdit"){
          //       if(this.selectCode.flag){
          //         this.form.uomForWeight=this.selectCode.unitCode
          //       }else{
          //         this.form.uomForVolume=this.selectCode.unitCode
          //       }
          // }
          console.info(val)
        },
        deep: true
      }
    },
    methods: {
      fuzhi(val){
        if(val.flag){
          this.form.uomForWeight=val.unitCode
        }else{
          this.form.uomForVolume=val.unitCode
        }
        console.info(this.form)
        // this.$emit("update:form", this.form);
        this.$forceUpdate()
      },
      /** 搜索按钮操作 */
      showList(value) {
        // this.getList();
        var str = "";
        if(value == "Weight"){
          str= "(重量)";
        }else if(value == "Volume"){
          str= "(体积)";
        }
        this.isounit.open = true;
        this.isounit.title = "选择运输单位类型"+str;
        this.isounit.unitType = value;
        this.$emit("update:isounit", this.isounit);
      },
      /** 搜索按钮操作 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.info(this.form.delFlag)
            if (this.form.transportUnitTypeId != undefined&&this.form.delFlag!= undefined) {
              console.info(this.form)
              updateTransport(this.form).then(response => {
                this.msgSuccess(response.msg);
                // this.open = false;
                this.getList();
              });
            }else {
              addTransport(this.form).then(response => {
                this.msgSuccess(response.msg);
                // this.open = false;
                this.getList();
              });
            }
            this.reset();
          }
        });
      },
      closeDialog(){
        this.reset();
      },
      /** 取消按钮操作 */
      cancel() {
        this.reset();
      },
      // 表单重置
      reset() {
        this.isOpen = false
        this.$emit("update:open", false);
        this.$emit("update:form", {});
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      getList() {
        this.$emit("getList",true);
      }
    }
  }
</script>

<style scoped>

</style>
